import Router from 'vue-router'
import App from '~/App.vue'
import Login from '~/pages/login/index.vue'
import Vue from 'vue'
import Home from '~/pages/home/index.vue'
import UserCenter from '~/pages/user-center/index.vue'
import Register from '~/pages/register/index.vue'
import Setp0 from '~/pages/register/step-0/index.vue'
import Setp1 from '~/pages/register/step-1/index.vue'
import Setp2 from '~/pages/register/step-2/index.vue'
import Setp3 from '~/pages/register/step-3/index.vue'
import Setp4 from '~/pages/register/step-4/index.vue'
import ForgetPass from '~/pages/forget-password/index.vue'

import Message from '~/pages/message/index.vue'
import MessageBill from '~/pages/message/bill/index.vue'
import MessageTrans from '~/pages/message/trans/index.vue'
import MessageSystem from '~/pages/message/system/index.vue'

const routes = [
  {
    path: '/',
    component: App,
    redirect: '/login',
    children: [
      {
        path: 'home',
        component: Home,
        name: '主页'
      },
      {
        path: 'bill-service',
        component: () => import(/* webpackChunkName: "bill-service" */ '~/pages/bill-service/index.vue'),
        name: '票据服务',
        children: [
          {
            path: 'financing',
            name: '票据融资',
            component: () => import(/* webpackChunkName: "bill-service" */ '~/pages/bill-service/financing/index.vue'),
            redirect: '/bill-service/financing/single',
            children: [
              {
                path: 'single',
                component: () => import(/* webpackChunkName: "bill-service" */ '~/pages/bill-service/financing/single/index.vue'),
                meta: {
                  title: '票据融资',
                  activeRoot: '/bill-service/financing'
                }
              },
              {
                path: 'batch',
                component: () => import(/* webpackChunkName: "bill-service" */ '~/pages/bill-service/financing/batch/index.vue'),
                meta: {
                  title: '票据融资',
                  activeRoot: '/bill-service/financing'
                }
              }
            ]
          },
          {
            path: 'purchase',
            component: () => import(/* webpackChunkName: "bill-service" */ '~/pages/bill-service/purchase/index.vue'),
            name: '票据收购',
            meta: {
              title: '票据收购',
              activeRoot: '/bill-service/purchase'
            }
          }
        ]
      },
      {
        path: 'transaction',
        component: () => import(/* webpackChunkName: "transaction" */ '~/pages/transaction/index.vue'),
        name: '交易管理',
        children: [
          {
            path: 'order',
            component: () => import(/* webpackChunkName: "transaction" */ '~/pages/transaction/order/index.vue'),
            name: '订单管理',
            redirect: '/transaction/order/list',
            meta: {
              title: '票据融资',
              activeRoot: '/transaction/order'
            },
            children: [
              {
                path: 'list',
                component: () => import(/* webpackChunkName: "transaction" */ '~/pages/transaction/order/list/index.vue'),
                meta: {
                  activeRoot: '/transaction/order'
                }
              },
              {
                path: 'detail/:id',
                component: () => import(/* webpackChunkName: "transaction" */ '~/pages/transaction/order/detail/index.vue'),
                meta: {
                  activeRoot: '/transaction/order'
                }
              }
            ]
          },
          {
            path: 'offer',
            name: '询价管理',
            component: () => import(/* webpackChunkName: "transaction" */ '~/pages/transaction/offer/index.vue'),
            redirect: '/transaction/offer/list',
            meta: {
              title: '票据融资',
              activeRoot: '/transaction/offer'
            },
            children: [
              {
                path: 'list',
                component: () => import(/* webpackChunkName: "transaction" */ '~/pages/transaction/offer/list/index.vue'),
                meta: {
                  activeRoot: '/transaction/offer'
                }
              },
              {
                path: 'detail/:id',
                component: () => import(/* webpackChunkName: "transaction" */ '~/pages/transaction/offer/detail/index.vue'),
                meta: {
                  activeRoot: '/transaction/offer'
                }
              }
            ]
          },

          {
            path: 'inventory',
            redirect: '/transaction/inventory/list',
            component: () => import(/* webpackChunkName: "transaction" */ '~/pages/transaction/inventory/index.vue'),
            name: '票据库存',
            children: [
              {
                path: 'list',
                component: () => import(/* webpackChunkName: "transaction" */ '~/pages/transaction/inventory/list/index.vue'),
                meta: {
                  activeRoot: '/transaction/inventory'
                }
              },
              {
                path: 'detail/:id',
                component: () => import(/* webpackChunkName: "transaction" */ '~/pages/transaction/inventory/detail/index.vue'),
                meta: {
                  activeRoot: '/transaction/inventory'
                }
              }
            ]
          }

        ]
      },
      {
        name: '账号管理',
        path: 'user-center',
        component: UserCenter,
        redirect: '/user-center/base',
        children: [
          {
            path: 'base',
            component: () => import(/* webpackChunkName: "user-center" */ '~/pages/user-center/base/index.vue'),
            redirect: '/user-center/base/base-register',
            name: '基本资料',
            children: [
              {
                path: 'base-register',
                component: () => import(/* webpackChunkName: "user-center" */ '~/pages/user-center/base/base-register/index.vue'),
                meta: {
                  activeRouter: '/user-center/base'
                }
              }
            ]
          },
          {
            path: 'account',
            component: () => import(/* webpackChunkName: "user-center" */ '~/pages/user-center/account/index.vue'),
            redirect: '/user-center/account/info',
            name: '兴业电子账户',
            children: [
              {
                path: 'info',
                component: () => import(/* webpackChunkName: "user-center" */ '~/pages/user-center/account/info/index.vue'),
                meta: {
                  activeRouter: '/user-center/account'
                }
              }
            ]
          },
          {
            path: 'safe',
            name: '安全设置',
            component: () => import(/* webpackChunkName: "user-center" */ '~/pages/user-center/safe/index.vue'),
            redirect: '/user-center/safe/password',
            children: [
              {
                path: 'password',
                component: () => import(/* webpackChunkName: "user-center" */ '~/pages/user-center/safe/password/index.vue'),
                meta: {
                  activeRouter: '/user-center/safe',
                  activeChildren: '/user-center/safe/password'
                }
              },
              {
                path: 'phone',
                component: () => import(/* webpackChunkName: "user-center" */ '~/pages/user-center/safe/phone/index.vue'),
                meta: {
                  activeRouter: '/user-center/safe',
                  activeChildren: '/user-center/safe/phone'
                }
              }
            ]
          },
          {
            path: 'auth',
            name: '实名认证',
            redirect: '/user-center/auth/step-1',
            component: () => import(/* webpackChunkName: "user-center" */ '~/pages/user-center/auth/index.vue'),
            children: [
              {
                path: 'step-1',
                component: () => import(/* webpackChunkName: "user-center" */ '~/pages/user-center/auth/step-1/index.vue'),
                meta: {
                  activeRouter: '/user-center/auth',
                  activeChildren: '/user-center/auth/step-1'
                }
              },
              {
                path: 'step-2',
                component: () => import(/* webpackChunkName: "user-center" */ '~/pages/user-center/auth/step-2/index.vue'),
                meta: {
                  activeRouter: '/user-center/auth',
                  activeChildren: '/user-center/auth/step-2'
                }
              },
              {
                path: 'step-3',
                component: () => import(/* webpackChunkName: "user-center" */ '~/pages/user-center/auth/step-3/index.vue'),
                meta: {
                  activeRouter: '/user-center/auth',
                  activeChildren: '/user-center/auth/step-3'
                }
              },
              {
                path: 'amount',
                component: () => import(/* webpackChunkName: "user-center" */ '~/pages/user-center/auth/amount/index.vue'),
                meta: {
                  activeRouter: '/user-center/auth',
                  activeChildren: '/user-center/auth/amount'
                }
              }
            ]
          }
        ]
      },
      {
        name: '消息提醒',
        path: 'message',
        redirect: '/message/bill',
        component: Message,
        children: [
          {
            name: '票据提醒',
            path: 'bill',
            component: MessageBill,
            meta: '/message/bill'
          },
          {
            name: '交易提醒',
            path: 'trans',
            component: MessageTrans,
            meta: '/message/trans'
          },
          {
            name: '系统提醒',
            path: 'system',
            component: MessageSystem,
            meta: '/message/system'
          }
        ]
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/forgetPass',
    name: 'forgetPass',
    component: ForgetPass
  },
  {
    path: '/register',
    name: '注册',
    redirect: '/register/step-0',
    component: Register,
    children: [
      {
        path: 'step-0',
        component: Setp0,
        name: '基本信息'
      },
      {
        path: 'step-1',
        component: Setp1,
        name: '企业认证'
      },
      {
        path: 'step-2',
        component: Setp2,
        name: '业务授权'
      },
      {
        path: 'step-3',
        component: Setp3,
        name: '账户绑定'
      },
      {
        path: 'step-4',
        component: Setp4,
        name: '注册成功'
      }
    ]
  }
]
export const router = new Router({
  mode: 'history',
  routes,
  scrollBehavior () {
    return {
      x: 0,
      y: 0
    }
  }
})

let freePage = ['login', 'register', 'forgetPass']
router.beforeEach((to, form, next) => {
  let token = window.sessionStorage.getItem('token')
  if (token) {
    next()
  } else {
    let currentRouter = to.fullPath.split('/')[1]
    if (freePage.indexOf(currentRouter) === -1) {
      Vue.prototype.$notify({
        title: '提示',
        message: '您的登录信息已失效,请重新登录',
        type: 'error'
      })
      next('/login')
    } else {
      next()
    }
  }
})
